import { useAppStore, useNoteStore, useSessionStore } from '@v-model/shared--stores'

const __TokenKey__ = 'Authorization'
const __UserKey__ = 'AuthUser'
const __ConfigKey__ = 'AppConfig'

export function GET_TOKEN_KEY() {
  return __TokenKey__
}

export function GET_TOKEN() {
  return sessionStorage.getItem(__TokenKey__)
}
export function SET_TOKEN(TOKEN: string) {
  const SessionStore = useSessionStore()
  SessionStore.SET_TOKEN(TOKEN)
  return sessionStorage.setItem(__TokenKey__, TOKEN)
}
export function REMOVE_TOKEN() {
  sessionStorage.removeItem(__TokenKey__)
}
export function SET_LOGON(LOGON: any) {
  const SessionStore = useSessionStore()
  SessionStore.SET_LOGON(LOGON)
  return sessionStorage.setItem(__UserKey__, JSON.stringify(LOGON))
}
export function GET_LOGON() {
  const LOGON = sessionStorage.getItem(__UserKey__)
  if (LOGON) {
    return JSON.parse(LOGON)
  } else {
    return null
  }
}
export function SET_APP_CONFIG(USER_ID: string, ROLE_ID: string, APP_CONFIG: any) {
  const AppStore = useAppStore()
  if (!APP_CONFIG) {
    APP_CONFIG = AppStore.GET_DEFAULT()
  }
  window.localStorage.setItem(__ConfigKey__ + '_' + USER_ID + '_' + ROLE_ID, JSON.stringify(APP_CONFIG))
  AppStore.SET_CONFIG(APP_CONFIG)
}
export function GET_APP_CONFIG(USER_ID: string, ROLE_ID: string) {
  const AppStore = useAppStore()
  const AppConfig = window.localStorage.getItem(__ConfigKey__ + '_' + USER_ID + '_' + ROLE_ID)
  if (AppConfig) {
    return JSON.parse(AppConfig)
  } else {
    return AppStore.GET_DEFAULT()
  }
}

export function SET_LOCK(USER_ID: string, islock: any) {
  const SessionStore = useSessionStore()
  SessionStore.SET_LOCK(islock)
  return sessionStorage.setItem(USER_ID, islock)
}
export function GET_LOCK(USER_ID: string) {
  return sessionStorage.getItem(USER_ID)
}

export function SET_NOTE(note: any) {
  const NoteStore = useNoteStore()
  NoteStore.SET_NOTE(note)
  return sessionStorage.setItem('__Note__', JSON.stringify(note))
}
export function GET_NOTE() {
  const note = sessionStorage.getItem('__Note__')
  if (note) {
    return JSON.parse(note)
  } else {
    return null
  }
}
export function SET_NOTE_KEY(key: string) {
  const NoteStore = useNoteStore()
  NoteStore.SET_NOTE_KEY(key)
  return sessionStorage.setItem('__NoteKey__', key)
}
export function GET_NOTE_KEY() {
  const key = sessionStorage.getItem('__NoteKey__')
  if (key) {
    return key
  } else {
    return null
  }
}
